package com.xbxkj.api.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xbxkj.commen.bean.Dictionaries;
import com.xbxkj.commen.controller.BaseController;
import com.xbxkj.commen.controller.helper.$;
import com.xbxkj.commen.controller.model.ResultModel;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 字典表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2019-08-19
 */
@Controller("AdminDictionariesController")
@RequestMapping("/admin/dictionaries")
public class DictionariesController extends BaseController {

    @RequestMapping("main.html")
    public String main(){
       return  "dictionaries/main";
    }

    @RequestMapping("addTree.html")
    public String addTree(){
       return  "dictionaries/addTree";
    }

    @RequestMapping("editTree.html")
    public String editTree(String id, Model model){
      model.addAttribute("info", dictionariesService.getById(id)) ;
       return  "dictionaries/editTree";
    }

    /**
     * 查询所有的字典数据
     * @param dictionaries
     * @return
     */
    @RequestMapping("finDictionariesAjax")
    @ResponseBody
    public ResultModel<?> finDictionariesAjax(Dictionaries dictionaries){


        return $.success(dictionariesService.finDictionariesAjax(dictionaries));
    }

    /**
     * 查询一个value值下的所有子集数据
     * @return
     */
    @RequestMapping("findDicByParentValueAjax")
    @ResponseBody
    public ResultModel<?>  findDicByParentValue(String parentValue){
        List<Dictionaries> list=dictionariesService.findDicByParentValue(parentValue);
        return $.success(list);
    }

    @RequestMapping("insertOrupdateTree")
    @ResponseBody
    public ResultModel<?>   insertOrupdateTree(Dictionaries dictionaries){
        QueryWrapper<Dictionaries> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("value",dictionaries.getValue());
        List<Dictionaries> list=dictionariesService.list(queryWrapper);

        if(list.size()>0){
            Dictionaries dbDictionaries=list.get(0);
            //如果修改的节点 与  数据库的节点不同 并且   有重复的存在 那么返回信息
            if(!dbDictionaries.getId().equals(dictionaries.getId())){
                return  $.error( "value值有重复，重复Name为："+list.get(0).getName());
            }
        };
        if(dictionariesService.saveOrUpdate(dictionaries)){
            return $.success( );
        }else{
            return  $.error("操作失败");
        }

    }
    @RequestMapping("delData")
    @ResponseBody
    public ResultModel<?>   delData(String  id){
        return dictionariesService.delData(id);
    }
}
